var appid = 'kou72um0ioak1kubrz0klwx8a7s3l261mgt54vbyotnblnby';
var appkey = 'o56bexx1vr522x7ygfkvi7efvbhhmo6x2oaf3txb75wi2509';
AV.initialize(appid, appkey);
var storage = window.localStorage;

mui.init();
var today = new Date();
var date_string = today.pattern('yyyy-MM-dd');
var storage_key = 'daily_' + date_string;
var say = '“各位观众，中秋节刚过，我给大家拜个晚年”';
if (storage_key in storage) {
	var daily_data = JSON.parse(storage[storage_key]);
	var content = daily_data['share_content'];
	var index = content.lastIndexOf('：');
	if (index != -1) {
		say = '“' + content.slice(index + 1) + '”';
	}
}
document.getElementById("miao-say").innerText = say;

//去好评
document.getElementById("rate").addEventListener('tap', function() {
	if (mui.os.ios) {
		location.href = 'https://itunes.apple.com/cn/app/id927289999';
	} else if (mui.os.android) {
		plus.runtime.openURL("market://details?id=com.fanhe.tee", function(e) {
			plus.runtime.openURL("market://details?id=com.fanhe.tee", function(e) {
				mui.alert("360手机助手和应用宝，你一个都没装，暂时无法评分，感谢支持");
			}, "com.qihoo.appstore");
		}, "com.tencent.android.qqdownloader");
	}
});

//去吐槽
document.getElementById("tucao").addEventListener('tap', function() {
	plus.runtime.openURL('http://t.fanheapp.com/tucao');
});

//版权声明
document.getElementById("copyright").addEventListener('tap', function() {
	plus.runtime.openURL('http://t.fanheapp.com/copyright');
});

//新手引导
document.getElementById("rookie").addEventListener('tap', function() {
	plus.runtime.openURL('http://t.fanheapp.com/newbie');
});

mui('#setting').on('tap', '.logout', function() {
	if (plus) {
		plus.nativeUI.confirm('确定退出？', function(e) {
			if (e.index == 0) {
				AV.User.logOut();
				plus.oauth.getServices(function(services) {
					var auths = services;
					for (var i in auths) {
						var s = auths[i];
						if (s.authResult) {
							s.logout();
						}
					}
				});
				for (var i = 0; i < storage.length; i++) {
					var storage_key = storage.key(i);
					if (/^badge_/g.test(storage_key) || /^scratch_/g.test(storage_key) || /^lottery_/g.test(storage_key)) {
						delete storage[storage_key];
					}
				}
				storage['var_login_status_changed'] = true;
				mui.toast('退出成功');
				delete storage['rec_dict'];
				delete storage['rec_array'];
				window.location.href = window.location.href;
			}
		}, '登出确认', ['退出', '取消']);
	}
});


mui('#setting').on('tap', '.clear', function() {
	plus.nativeUI.showWaiting("清除缓存中")
	for (var i = 0; i < storage.length; i++) {
		var storage_key = storage.key(i);
		if (/^daily_/g.test(storage_key) || /^rec_/g.test(storage_key ||
				/^topic_/g.test(storage_key) || /^frame_/g.test(storage_key))) {
			delete storage[storage_key];
		} else if (/^img_/g.test(storage_key)) {
			plus.io.resolveLocalFileSystemURL(storage_key, function(entry) {
				entry.remove();
			}, function(error) {});
			delete storage[storage_key];
		}
	}
	plus.cache.clear( function () {
		plus.nativeUI.closeWaiting();
		var now = new Date();
		storage["clear_cache_at"] = now.getTime() + 6* 86400000;
		console.log("清除后数据缓存保持到" + storage["clear_cache_at"]);
		mui.toast('清除完毕');
		plus.cache.calculate(function (size) {
			var mb_size = Math.floor(size / 1048576);
			document.getElementById("mb-size").innerText = mb_size + 'MB';
		});
	});
});

mui.plusReady(function() {
	plus.webview.currentWebview().addEventListener("show", function(e) {
		if (storage['var_login_status_changed'] == 'true') {
			storage['var_login_status_changed'] = false;
			window.location.href = window.location.href;
		}
	}, false);

	//设定状态栏样式
	if (plus.navigator.isImmersedStatusbar() && mui.os.ios) {
		document.getElementById("bar-cover").style.display = 'block';
		document.getElementById("bar-cover").style.backgroundImage = "";
	}
	if (plus.navigator.isImmersedStatusbar() && mui.os.android) {
		document.getElementById("bar-cover").style.display = 'block';
		document.getElementById("bar-cover").style.backgroundColor = "";
	}

	mui('.mui-scroll-wrapper').scroll({
		deceleration: 0.006 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
	});

	mui('#setting').on('tap', '#gift', function() {
		mui.openWindow({
			id: 'gift',
			url: 'gift.html',
			styles: {
				top: 0,
				bottom: 0,
				bounce: "none",
			},
			show: {
				autoShow: true,
			},
			waiting: {
				autoShow: true,
				title: "校验徽章中"
			}
		});
	});
	
	mui('#setting').on('tap', '#my-gift', function() {
		mui.openWindow({
			id: 'my_gift',
			url: 'my_gift.html',
			styles: {
				top: 0,
				bottom: 0,
				bounce: "none",
			},
			show: {
				autoShow: true,
			},
			waiting: {
				autoShow: true,
				title: "查询奖品中"
			}
		});
	});

	var user = AV.User.current();
	if (!user) {
		mui("#user-info")[0].style.display = "none";
		mui("#logout-button")[0].style.display = "none";
		mui("#gift-delivery")[0].style.display = "none";
	}

	plus.runtime.getProperty(plus.runtime.appid, function(inf) {
		wgtVer = inf.version;
		mui("#app-version")[0].innerHTML = "V" + wgtVer;
	});
	
	plus.cache.calculate(function (size) {
		var mb_size = Math.floor(size / 1048576);
		document.getElementById("mb-size").innerText = mb_size + 'MB';
	});
});